# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=tcl

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.6.1
pkgrel=3
pkgdesc="The Tcl scripting language (mingw-w64)"
arch=('any')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip' )
license=("custom")
url="http://tcl.sourceforge.net"
source=("http://downloads.sourceforge.net/sourceforge/tcl/tcl${pkgver}-src.tar.gz"
    002-fix-forbidden-colon-in-paths.mingw.patch
    003-fix-redefinition.mingw.patch
    004-use-system-zlib.mingw.patch
    005-no-xc.mingw.patch
    006-proper-implib-name.mingw.patch
    007-install.mingw.patch
    008-tcl-8.5.14-hidden.patch
    009-fix-using-gnu-print.patch)

md5sums=('aae4b701ee527c6e4e1a6f9c7399882e'
         '04e6b9f4e67ff4d23e60b2aa56777d5a'
         '9c6a6e49d861a88530707b99a55efc7d'
         'f9fd38a4bac21c094e48f7f1585d5412'
         '312640178d8e976aa64c634c92685fc0'
         '84e1e16b396e875e67203ac6956658e7'
         '7f05d7e5c74939d6c1ccd50f30c53f2f'
         'f700b69022005a8d6fa3a4d5929af5ed'
         '8920e0fa2e32acaa620992c1597c98d4')

prepare() {
  cd "${srcdir}/tcl${pkgver}"
  patch -Np1 -i "${srcdir}/002-fix-forbidden-colon-in-paths.mingw.patch"
  patch -Np1 -i "${srcdir}/003-fix-redefinition.mingw.patch"
  patch -Np1 -i "${srcdir}/004-use-system-zlib.mingw.patch"
  patch -Np1 -i "${srcdir}/005-no-xc.mingw.patch"
  patch -Np1 -i "${srcdir}/006-proper-implib-name.mingw.patch"
  patch -Np1 -i "${srcdir}/007-install.mingw.patch"
  patch -Np1 -i "${srcdir}/008-tcl-8.5.14-hidden.patch"
  patch -Np1 -i "${srcdir}/009-fix-using-gnu-print.patch"

  cd win && autoreconf -fi
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf "$srcdir/tcl$pkgver/" "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  [ "${MINGW_CHOST}" = "x86_64-w64-mingw32" ] && enable64bit='--enable-64bit'
  "${srcdir}"/${_realname}${pkgver}/win/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-threads \
    $enable64bit
  make -j1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  cp tclsh86.exe tclsh.exe
  #PATH=.:$PATH 
  make install INSTALL_ROOT="$pkgdir"

  cp "${pkgdir}${MINGW_PREFIX}/bin/tclsh86.exe" "${pkgdir}${MINGW_PREFIX}/bin/tclsh.exe"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  find "${pkgdir}${MINGW_PREFIX}" -name '*.sh' -o -name '*.dll' -o -name '*.exe' | xargs -rtl1 chmod 755

  local _libver=${pkgver%.*}
  _libver=${_libver//./}

  sed \
    -e "s|^\(TCL_BUILD_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtcl${_libver}.dll.a'|" \
    -e "s|^\(TCL_SRC_DIR\)='.*'|\1='${MINGW_PREFIX}/include/tcl${pkgver%.*}/tcl-private'|" \
    -e "s|^\(TCL_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtclstub${_libver}.a'|" \
    -e "s|^\(TCL_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib'|" \
    -e "s|^\(TCL_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib -ltclstub${_libver}'|" \
    -e "s|^\(TCL_INCLUDE_SPEC\)='.*|\1='-I${MINGW_PREFIX}/include/tcl${pkgver%.*}'|" \
    -i "${pkgdir}${MINGW_PREFIX}/lib/tclConfig.sh"

  sed \
    -e "s|^\(tdbc_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/tdbc1.0.0 -ltdbcstub100'|" \
    -e "s|^\(TDBC_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/tdbc1.0.0 -ltdbcstub100'|" \
    -e "s|^\(tdbc_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/tdbc1.0.0/libtdbcstub100.a'|" \
    -e "s|^\(TDBC_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/tdbc1.0.0/libtdbcstub100.a'|" \
    -e "s|^\(tdbc_INCLUDE_SPEC\)='.*|\1='${MINGW_PREFIX}/lib/tdbc1.0.0/libtdbcstub100.a'|" \
    -e "s|^\(tdbc_INCLUDE_SPEC\)='.*|\1='${MINGW_PREFIX}/lib/tdbc1.0.0/libtdbcstub100.a'|" \
    -i "${pkgdir}${MINGW_PREFIX}/lib/tdbc1.0.0/tdbcConfig.sh"

  sed \
    -e "s|^\(itcl_BUILD_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl4.0.0 -litcl400'|" \
    -e "s|^\(ITCL_BUILD_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl4.0.0 -litcl400'|" \
    -e "s|^\(itcl_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl4.0.0 -litclstub400'|" \
    -e "s|^\(ITCL_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl4.0.0 -litclstub400'|" \
    -e "s|^\(itcl_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/itcl4.0.0/libitclstub400.a'|" \
    -e "s|^\(ITCL_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/itcl4.0.0/libitclstub400.a'|" \
    -i "${pkgdir}${MINGW_PREFIX}/lib/itcl4.0.0/itclConfig.sh"

  ln -s "${pkgdir}${MINGW_PREFIX}/lib/libtcl86.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libtcl.dll.a"
  ln -s "${pkgdir}${MINGW_PREFIX}/lib/tclConfig.sh" "${pkgdir}${MINGW_PREFIX}/lib/tcl${pkgver%.*.*}/tclConfig.sh"

  # Install private headers
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/tcl${pkgver%.*}/tcl-private/"{generic,win}
  find generic win -name "*.h" -exec cp -p '{}' "${pkgdir}${MINGW_PREFIX}"/include/tcl${pkgver%.*}/tcl-private/'{}' ';'
  ( cd "${pkgdir}${MINGW_PREFIX}/include"
    for i in *.h ; do
      cp -f $i ${pkgdir}${MINGW_PREFIX}/include/tcl${pkgver%.*}/tcl-private/generic/
    done
  )
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/tcl${pkgver%.*}/encoding/"*.enc
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/"*/pkgIndex.tcl

  cp -rf ${pkgdir}${MINGW_PREFIX}/man ${pkgdir}${MINGW_PREFIX}/share/
  rm -rf ${pkgdir}${MINGW_PREFIX}/man
  install -Dm644 ${srcdir}/${_realname}${pkgver}/win/tcl.m4 ${pkgdir}${MINGW_PREFIX}/share/aclocal/tcl${pkgver%.*}.m4
}
